/**
 * 点到线的最短距离实际上就是点到线段的垂直距离。 注意是线段
 * position为点的坐标
 * point1为线段上点的坐标
 * point2为线段另外一点的坐标
 * 将点连接上两个线段的
 */
export const PointToSegDist = function(point1, point2, position) {
  //三角形三个边长
  var x1=point1.x;
  var y1=point1.y;
  var x2=point2.x;
  var y2=point2.y;
  var x=position.x;
  var y=position.y;
  var cross= (x2 - x1) * (x - x1) + (y2 - y1) * (y - y1);
  if (cross <= 0) return Math.sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
  var d2 = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
  if (cross >= d2) return Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
  var r = cross / d2;
  var px = x1 + (x2 - x1) * r;
  var py = y1 + (y2 - y1) * r;
  return Math.sqrt((x - px) * (x - px) + (py - y) * (py - y));
};

export const ifPointOnSegment=function(point1,point2,position) {
  //弃用
  //判断position是否在由p1和p2组成的线段上，容许一定误差 0.1
  var xa = point2.x - point1.x;
  var ya = point2.y - point1.y;
  var xb = position.x - point1.x;
  var yb = position.y - point1.y;
  return (Math.abs(yb * xa - ya * xb) <= 0.1
    && Math.min(point1.x, point2.x) <= position.x
    && Math.max(point1.x, point2.x) >= position.x
    && Math.min(point1.y, point2.y) <= position.y
    && Math.max(point1.y, point2.y) >= position.y);
};

export const pointset=[{x:444, y:121},{x:612, y:110},{x:779, y:98},{x:947, y:83},{x:1114, y:67},
                      {x:444, y:343}, {x:779, y:340}, {x:1114, y:336}, {x:444, y:479}, {x:444, y:495},
                      {x:444, y:524}, {x:444, y:553}, {x:444, y:582}, {x:444, y:611}, {x:444, y:640},
                      {x:444, y:669}, {x:444, y:698}, {x:444, y:727}, {x:444, y:756}, {x:444, y:785},
                      {x:444, y:801}, {x:779, y:479}, {x:779, y:495}, {x:779, y:524}, {x:779, y:553},
                      {x:779, y:582}, {x:779, y:611}, {x:779, y:640}, {x:779, y:669}, {x:779, y:698},
                      {x:779, y:727}, {x:779, y:756}, {x:779, y:785}, {x:779, y:801}, {x:1114, y:479},
                      {x:1114, y:495}, {x:1114, y:524}, {x:1114, y:553}, {x:1114, y:582}, {x:1114, y:611},
                      {x:1114, y:640}, {x:1114, y:669}, {x:1114, y:698}, {x:1114, y:727}, {x:1114, y:756},
                      {x:1114, y:785}, {x:1114, y:801}, {x:480, y:466}, {x:480, y:479}, {x:740, y:466},
                      {x:740, y:479}, {x:610, y:433}, {x:612, y:495}, {x:612, y:524}, {x:612, y:553},
                      {x:612, y:582}, {x:612, y:611}, {x:612, y:640}, {x:612, y:669}, {x:612, y:698},
                      {x:612, y:727}, {x:612, y:756}, {x:612, y:785}, {x:947, y:495}, {x:947, y:524},
                      {x:947, y:553}, {x:947, y:582},{x:947, y:611},{x:947, y:640},{x:947, y:669},
                      {x:947, y:698},{x:947, y:727},{x:947, y:756},{x:947, y:785}];

export const getRandomColor = function (){//十六进制颜色随机
  var r = Math.floor(Math.random()*256);
  var g = Math.floor(Math.random()*256);
  var b = Math.floor(Math.random()*256);
  var color = '#'+r.toString(16)+g.toString(16)+b.toString(16);
  return color;
};

function randomHsl(){
  var H = Math.random();
  var S = Math.random();
  var L = Math.random();
  return [H, S, L];
}

function getHslArray(num) {
  var HSL = [];
  for (var i = 0; i < num; i++) {
    var ret = randomHsl();
    // 颜色相邻颜色差异须大于 0.25
    if (i > 0 && Math.abs(ret[0] - HSL[i - 1][0]) < 0.25) {
      i--;
      continue; // 重新获取随机色
    }
    ret[1] = 0.7 + (ret[1] * 0.2); // [0.7 - 0.9] 排除过灰颜色
    ret[2] = 0.4 + (ret[2] * 0.4); // [0.4 - 0.8] 排除过亮过暗色

    // 数据转化到小数点后两位
    ret = ret.map(function (item) {
      return parseFloat(item.toFixed(2));
    });

    HSL.push(ret);
  }
  return HSL;
}

function hslToRgb(H, S, L) {
  var R, G, B;
  if (+S === 0) {
    R = G = B = L; // 饱和度为0 为灰色
  } else {
    var hue2Rgb = function (p, q, t) {
      if (t < 0) t += 1;
      if (t > 1) t -= 1;
      if (t < 1/6) return p + (q - p) * 6 * t;
      if (t < 1/2) return q;
      if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
      return p;
    };
    var Q = L < 0.5 ? L * (1 + S) : L + S - L * S;
    var P = 2 * L - Q;
    R = hue2Rgb(P, Q, H + 1/3);
    G = hue2Rgb(P, Q, H);
    B = hue2Rgb(P, Q, H - 1/3);
  }
  return [Math.round(R * 255), Math.round(G * 255), Math.round(B * 255)];
}

function rgbtoHex(r,g,b) {
  var hex = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
  return hex;
}

export const getRgbArray = function(num) {
  if (!num) return [];
  var hslArray=getHslArray(num);
  var rgbArray = hslArray.map(function (item) {
    return hslToRgb.apply(this, item);
  });

  return rgbArray.map(function (item){
    return rgbtoHex.apply(this, item);
  });
};
